package org.poseidon.report.service.impl;

import org.poseidon.common.exception.ServiceException;
import org.poseidon.common.utils.DateUtils;
import org.poseidon.common.utils.SecurityUtils;
import org.poseidon.report.domain.BuMilestone;
import org.poseidon.report.domain.vo.BuMilestoneVO;
import org.poseidon.report.mapper.BuMilestoneMapper;
import org.poseidon.report.service.IBuDiaryService;
import org.poseidon.report.service.IBuMilestoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 里程碑Service业务层处理
 * 
 * @author gfc
 * @date 2022-02-22
 */
@Service
public class BuMilestoneServiceImpl implements IBuMilestoneService 
{
    @Autowired
    private BuMilestoneMapper buMilestoneMapper;

    @Autowired
    private IBuDiaryService buDiaryService;
    /**
     * 查询里程碑
     * 
     * @param id 里程碑主键
     * @return 里程碑
     */
    @Override
    public BuMilestoneVO selectBuMilestoneById(Long id)
    {
       return buMilestoneMapper.selectBuMilestoneById(id);
    }

    /**
     * 查询里程碑列表
     * 
     * @param buMilestone 里程碑
     * @return 里程碑
     */
    @Override
    public List<BuMilestoneVO> selectBuMilestoneList(BuMilestoneVO buMilestone)
    {
        return buMilestoneMapper.selectBuMilestoneList(buMilestone);
    }

    /**
     * 新增里程碑
     * 
     * @param buMilestone 里程碑
     * @return 结果
     */
    @Override
    public int insertBuMilestone(BuMilestone buMilestone)
    {
        buMilestone.setCreateTime(DateUtils.getNowDate());
        buMilestone.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return buMilestoneMapper.insertBuMilestone(buMilestone);
    }

    /**
     * 修改里程碑
     * 
     * @param buMilestone 里程碑
     * @return 结果
     */
    @Override
    public int updateBuMilestone(BuMilestone buMilestone)
    {
        buMilestone.setUpdateTime(DateUtils.getNowDate());
        return buMilestoneMapper.updateBuMilestone(buMilestone);
    }

    /**
     * 批量删除里程碑
     * 
     * @param ids 需要删除的里程碑主键
     * @return 结果
     */
    @Override
    public int deleteBuMilestoneByIds(List<Long> ids)
    {
       int count =  buDiaryService.countDiaryByMilestoneList(ids);
       if(count>0) {
           throw  new ServiceException("有周报关联",20010);
       }
        return buMilestoneMapper.deleteBuMilestoneByIds(ids);
    }

    /**
     * 删除里程碑信息
     * 
     * @param id 里程碑主键
     * @return 结果
     */
    @Override
    public int deleteBuMilestoneById(Long id)
    {
        return buMilestoneMapper.deleteBuMilestoneById(id);
    }

    @Override
    public void batchInsertMilestone(List<BuMilestone> milestones) {
        buMilestoneMapper.batchInsertMilestone(milestones);
    }
}
